package ar.com.ford.it.fullEconomics.persistence.dao.query;

import java.sql.Timestamp;
import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp01Part;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class FindCommodPartsByPartQuery implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder,
                                       Object[] parameters) {
        Expression exp = null;
        Teesp01Part part = (Teesp01Part) parameters[0];
        String mode = (String) parameters[1];
        
        Teesg01Plant plant = part.getEesp01PlantCodeC();
        String prefix = part.getEesp01PartPrefixCodeC();
        String base = part.getEesp01PartBaseCodeC();
        String suffix = part.getEesp01PartSuffixCodeC();
        Timestamp maxDate = FullEconomicsDateUtil.maxDate();

        exp = builder.get("eesp03PlantCodeC").equal(plant);
        if(!mode.equals("history"))
            exp = exp.and(builder.get("eesp03CommodPartOutDateY").equal(maxDate));
        if(prefix != null){
            prefix += "%";
            exp = exp.and(builder.get("teesp01Part").get("eesp01PartPrefixCodeC").like(prefix));
        }
        if(base != null){
            base += "%";
            exp = exp.and(builder.get("teesp01Part").get("eesp01PartBaseCodeC").like(base));
        }
        if(suffix != null){
            suffix += "%";
            exp = exp.and(builder.get("teesp01Part").get("eesp01PartSuffixCodeC").like(suffix));   
        }
        return exp;
    }

}
